package com.ibm.law.ui.system.setup;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.beans.IBeanValueProperty;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.property.Properties;
import org.eclipse.core.databinding.property.value.IValueProperty;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.databinding.viewers.CellEditorProperties;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.jface.databinding.viewers.ObservableValueEditingSupport;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnViewerEditor;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TableViewerEditor;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import com.ibm.law.domain.Category;
import com.ibm.law.domain.Template;
import com.ibm.law.domain.TemplateList;
import com.ibm.law.service.system.CategoryService;
import com.ibm.law.service.system.TemplateService;
import com.ibm.law.ui.AppWindow;
import com.ibm.law.ui.common.CloseDialogSelectionAdapter;
import com.ibm.law.ui.common.SelectionUpdateValueStrategy;

public class TemplateSetup extends Composite {
	private DataBindingContext m_bindingContext;
	private Table table;
	private java.util.List<Category> categories;
	private ListViewer listViewer;
	private TemplateList templateList = new TemplateList();
	private Category category;
	private TableViewer tableViewer;
	private TableViewerColumn tableViewerColumn;
	private Button btnNewButton;
	private Button btnNewButton_2;
	private TemplateService templateService=new TemplateService();

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public TemplateSetup(Composite parent, int style) {
		super(parent, style);
		setLayout(new GridLayout(2, false));
		CategoryService categoryService = new CategoryService();
		categories = categoryService.getAll();
		listViewer = new ListViewer(this, SWT.BORDER | SWT.V_SCROLL);
		List list = listViewer.getList();
		list.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				IStructuredSelection selection=(IStructuredSelection)listViewer.getSelection();
				category=(Category)selection.getFirstElement();
				templateList.setList(templateService.findTempalteByCategory(category));
			}
		});
		list.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1));

		tableViewer = new TableViewer(this, SWT.BORDER | SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.setHeaderVisible(true);
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(
				tableViewer) {
			protected boolean isEditorActivationEvent(
					ColumnViewerEditorActivationEvent event) {
				return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
						|| event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
						|| event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
			}
		};

		TableViewerEditor.create(tableViewer, actSupport,
				ColumnViewerEditor.TABBING_HORIZONTAL
						| ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
						| ColumnViewerEditor.TABBING_VERTICAL
						| ColumnViewerEditor.KEYBOARD_ACTIVATION);

		tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn = tableViewerColumn.getColumn();
		tblclmnNewColumn.setWidth(343);
		tblclmnNewColumn.setText("鉴定模版");

		Composite composite = new Composite(this, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 2, 1));
		composite.setLayout(new RowLayout(SWT.HORIZONTAL));

		btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.setEnabled(false);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TemplateDataDialog dialog=new TemplateDataDialog(AppWindow.getInstance().getShell(), SWT.DIALOG_TRIM
						| SWT.APPLICATION_MODAL);
				dialog.open();
			}
		});
		btnNewButton.setText("编辑");

		Button btnNewButton_1 = new Button(composite, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Template template = new Template();
				templateList.add(template);
				tableViewerColumn.getViewer().editElement(template, 0);
			}
		});
		btnNewButton_1.setText("增加");

		btnNewButton_2 = new Button(composite, SWT.NONE);
		btnNewButton_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				IStructuredSelection selection=(IStructuredSelection)tableViewer.getSelection();
				Template template=(Template)selection.getFirstElement();
				
				templateService.delete(template);
				templateList.remove(template);
			}
		});
		btnNewButton_2.setEnabled(false);
		btnNewButton_2.setText("删除");

		Button btnNewButton_3 = new Button(composite, SWT.NONE);
		btnNewButton_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				for(Template t:templateList.getList()){
					t.setCategoryId(category.getId());
				}
				templateService.saveAll(templateList.getList());
			}
		});
		btnNewButton_3.setText("保存");

		Button btnNewButton_4 = new Button(composite, SWT.NONE);
		btnNewButton_4.addSelectionListener(new CloseDialogSelectionAdapter());
		btnNewButton_4.setText("退出");
		m_bindingContext = initDataBindings();

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		ObservableListContentProvider listContentProvider = new ObservableListContentProvider();
		IObservableMap observeMap = BeansObservables.observeMap(listContentProvider.getKnownElements(), Category.class, "name");
		listViewer.setLabelProvider(new ObservableMapLabelProvider(observeMap));
		listViewer.setContentProvider(listContentProvider);
		//
		IObservableList selfList = Properties.selfList(Category.class).observe(categories);
		listViewer.setInput(selfList);
		//
		ObservableListContentProvider listContentProvider_1 = new ObservableListContentProvider();
		IObservableMap observeMap_1 = BeansObservables.observeMap(listContentProvider_1.getKnownElements(), Template.class, "name");
		tableViewer.setLabelProvider(new ObservableMapLabelProvider(observeMap_1));
		tableViewer.setContentProvider(listContentProvider_1);
		//
		IObservableList listTemplateListObserveList = BeanProperties.list("list").observe(templateList);
		tableViewer.setInput(listTemplateListObserveList);
		//
		CellEditor cellEditor = new TextCellEditor(tableViewer.getTable());
		IValueProperty cellEditorProperty = CellEditorProperties.control().value(WidgetProperties.text(SWT.Modify));
		IBeanValueProperty valueProperty = BeanProperties.value("name");
		tableViewerColumn.setEditingSupport(ObservableValueEditingSupport.create(tableViewer, bindingContext, cellEditor, cellEditorProperty, valueProperty));
		//
		IObservableValue observeSingleSelectionIndexTableObserveWidget = WidgetProperties.singleSelectionIndex().observe(table);
		IObservableValue observeEnabledBtnNewButtonObserveWidget_1 = WidgetProperties.enabled().observe(btnNewButton);
		bindingContext.bindValue(observeSingleSelectionIndexTableObserveWidget, observeEnabledBtnNewButtonObserveWidget_1, new SelectionUpdateValueStrategy(), new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER));
		//
		IObservableValue observeSingleSelectionIndexTableObserveWidget_1 = WidgetProperties.singleSelectionIndex().observe(table);
		IObservableValue observeEnabledBtnNewButton_2ObserveWidget = WidgetProperties.enabled().observe(btnNewButton_2);
		bindingContext.bindValue(observeSingleSelectionIndexTableObserveWidget_1, observeEnabledBtnNewButton_2ObserveWidget, new SelectionUpdateValueStrategy(), new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER));
		//
		return bindingContext;
	}
}
